<template>
  <!--自定义导航栏 -->
  <CustomeNavBar />

  <scroll-view class="scroll-view" refresher-enabled @refresherrefresh="onRefreshrefresh"
    :refresher-triggered="isTriggered" scroll-y @scrolltolower="onScrolltolower">

    <PageSkeleton v-if="isLoading" />

    <template v-else>
      <!-- 自定义轮播图 -->
      <WtdSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <WtdGuess ref="guessRef" />
      <view class="index">
        index
      </view>
    </template>

  </scroll-view>

</template>
<script setup lang="ts">
import CustomeNavBar from './components/CustomeNavBar.vue';
import CategoryPanel from './components/CategoryPanel.vue';
import HotPanel from './components/HotPanel.vue'
import { getHomeBannerApi, getHomeCategoryApi, getHomeHotAPI } from "@/services/home"
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import type { BannerItem, CategoryItem, HotItem } from '@/types/home';
import {useGuessList} from "@/composables/index"
import PageSkeleton from './components/PageSkeleton.vue';

//是否开启下拉刷新的标记
const isTriggered = ref<boolean>(false);
//是否加载中的标记
const isLoading = ref<boolean>(false);
//暂时的处理
const bannerList = ref<BannerItem[]>([]);
const getHomeBannerData = async () => {
  let res = await getHomeBannerApi()
  bannerList.value = res.result
}

//获取前台的分类数据
const categoryList = ref<CategoryItem[]>([]);
const getHomeCategoryData = async () => {
  let res = await getHomeCategoryApi()
  categoryList.value = res.result
}

//获取热门推荐数据
const hotList = ref<HotItem[]>([]);
const getHomeHotData = async () => {
  let res = await getHomeHotAPI()
  hotList.value = res.result
}

//猜你喜欢的组合式函数的调用
const {guessRef,onScrolltolower} = useGuessList();


//自定义下拉刷新的回调函数
const onRefreshrefresh = async () => {
  //开始动画
  isTriggered.value = true;
  //重新获取数据
  //  await getHomeBannerData();
  //  await getHomeCategoryData();
  //  await getHomeHotData();
  //重置猜你喜欢的数据
  guessRef.value?.resetData();
  //同时发送请求
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData(), guessRef.value?.getMore()]);
  //结束动画
  isTriggered.value = false;
}

onLoad(async () => {

  isLoading.value = true;
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false;

})
</script>

<style lang="scss">
page {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f7f7f7;
}

.scroll-view {
  flex: 1;
}
</style>
